﻿<?php

use yii\helpers\Html;
use yii\helpers\Url;
use yii\grid\GridView;
use yii\jui\DatePicker;
use yii\bootstrap\Modal;
use yii\bootstrap\Alert;
use yii\widgets\LinkPager;

$this->title = "任务列表";
Modal::begin([
    'id' => 'common-modal',
    'header' => '<h4 class="modal-title"></h4>',
    'footer' => '<a href="#" class="btn btn-primary" data-dismiss="modal">Close</a>',
]);


$js = <<<JS
$(".modaldialog").click(function(){ 
        aUrl = $(this).attr('data-url');
        aTitle = $(this).attr('data-title');
        console.log(aTitle);
        console.log(aUrl);
        
        $($(this).attr('data-target')+" .modal-title").text(aTitle);
        $($(this).attr('data-target')).modal("show")
             .find(".modal-body")
             .load(aUrl);
        return false;
   }); 
JS;
$this->registerJs($js);

Modal::end();
?>
<div class="RightCenter clear">

    <div class="RightTime">
        <div class="Time">
            <h3>任务列表</h3>
            
            <div class="input-prepend input-group right_Time">
                <li class="Read_LisJ">
                    <span data-type="1" <?php echo $select['daily_type'] == 1 ? 'class="Read_Span"' : '';?>>日</span>
                    <span data-type="2" <?php echo $select['daily_type'] == 2 ? 'class="Read_Span"' : '';?>>周</span>
                    <span data-type="3" <?php echo $select['daily_type'] == 3 ? 'class="Read_Span"' : '';?>>月</span>
                    <span data-type="4" <?php echo $select['daily_type'] == 4 ? 'class="Read_Span"' : '';?>>全部</span>
                </li>
                <span class="add-on input-group-addon Read_IN"></span>
                <input type="text" readonly  name="birthday" id="reservation" class="form-control form-controlCLOC"/>
            </div>
        </div>
    </div>
    <div class="row clear" style="margin-left: 1%;">
        <div class="count count_One">
            <k>任务与收入统计</k>
            <div>
                5000
                <?php // echo (Bill::getReChargeMoney($type = 1)['pay_money']>0?Bill::getReChargeMoney($type = 1)['pay_money']:0); ?>
                <h5>完成任务 (次数)</h5>
            </div>
            <div>
                50
                <?php // echo (Bill::getReChargeMoney($type = 1)['pay_money']>0?Bill::getReChargeMoney($type = 1)['pay_money']:0); ?>
                <h5>实际收入 (元)</h5>
            </div>
        </div>
        <div class="count count_Tow">
            <k>金币统计</k>
            <div>
                5000
                <?php // echo (Bill::getReChargeMoney($type = 1)['pay_money']>0?Bill::getReChargeMoney($type = 1)['pay_money']:0); ?>
                <h5>合计奖励 (金币)</h5>
            </div>
            <div>
                50
                <?php // echo (Bill::getReChargeMoney($type = 1)['pay_money']>0?Bill::getReChargeMoney($type = 1)['pay_money']:0); ?>
                <h5>规则抵消 (金币)</h5>
            </div>
            <div>
                50
                <?php // echo (Bill::getReChargeMoney($type = 1)['pay_money']>0?Bill::getReChargeMoney($type = 1)['pay_money']:0); ?>
                <h5>实际支出 (金币)</h5>
            </div>
        </div>

        <div class="count count_three">
            <k>支出统计</k>
            <div>
                5000
                <?php // echo (Bill::getReChargeMoney($type = 1)['pay_money']>0?Bill::getReChargeMoney($type = 1)['pay_money']:0); ?>
                <h5>零钱自动兑换 (元)</h5>
            </div>
            <div>
                50
                <?php // echo (Bill::getReChargeMoney($type = 1)['pay_money']>0?Bill::getReChargeMoney($type = 1)['pay_money']:0); ?>
                <h5>审批中 (元)</h5>
            </div>
            <div>
                50
                <?php // echo (Bill::getReChargeMoney($type = 1)['pay_money']>0?Bill::getReChargeMoney($type = 1)['pay_money']:0); ?>
                <h5>提现已通过 (元)</h5>
            </div>
        </div>

        <div class="count count_four">
            <k>兑换汇率</k>
            <div>
                <h1>今日汇率</h1>
                <h3 style="float:right;">0.66<button>设置</button></h3>
            </div>
        </div>
    </div>
    <div class="Read_date" style="margin-top:40px;margin-bottom: 5px;height:60px;">
        <ul>
            <li class="Read_LisJ" style="margin-top:12.5px;margin-left: 20px;">
                <a href="/task/create" class="btn btn-primary">新建任务</a>
            </li>
            <li class="Read_LisJ" style="margin-top:12.5px;">
                <div class="Seek">
                    <input type="text" class="form-control AppIN" id="title" placeholder="搜索标题" value="<?php echo isset($select['title']) ? $select['title'] : '';?>">
                    <button type="button" class="btn btn-primary SS">搜索</button>
                </div>

                <div class="SX clear" style="margin-left: 70px;">筛选:
                    <select id="type">
                        <option value="">全部类型</option>
                        <?php foreach($taskTypeList as $v):?>
                            <option <?php echo isset($select['type']) && $select['type'] == $v['id'] ? 'selected="selected"' : '';?> value="<?php echo $v['id'];?>"><?php echo $v['name'];?></option>
                        <?php endforeach;?>
                    </select>
                    <button type="button" class="btn btn-primary czS">重置</button>
                </div>
            </li>
            <li class="Read_LisJ" style="float: right;margin-top:12.5px;margin-right: 20px;">
                <div class="Seek">
                    <button class="btn btn-primary DD">批量关闭</button>
                </div>
            </li>
        </ul>
    </div>
    <?php
    if (Yii::$app->getSession()->hasFlash('success')) {
        echo Alert::widget([
            'options' => [
                'class' => 'alert-success', //这里是提示框的class
                'style' => 'width:96%;margin:0 auto;',
            ],
            'body' => Yii::$app->getSession()->getFlash('success'), //消息体
        ]);
    }
    if (Yii::$app->getSession()->hasFlash('error')) {
        echo Alert::widget([
            'options' => [
                'class' => 'alert-error',
                'style' => 'width:96%;margin:0 auto;',
            ],
            'body' => Yii::$app->getSession()->getFlash('error'),
        ]);
    }
    ?>
    <?=
    GridView::widget([
        'layout' => "{items}",
        'dataProvider' => $dataProvider,
//        'id' => 'grid',
        'options' => [ 'style' => 'text-align:center;' , 'id' => 'grid' ],
        'columns' => [
            [
                'class' => 'yii\grid\CheckboxColumn',
                'name' => 'id',
                'headerOptions' => [ 'style' => 'text-align:center;' , 'width' => 10 ],
                'contentOptions' => ['width' => 10],
                'checkboxOptions' => function ($model, $key, $index, $column) {
                    return ['value' => $model['id']];
                }
            ],
            [
                'attribute' => 'task_title',
                'label' => '任务标题',
                'headerOptions' => [ 'style' => 'text-align:center;' , 'width' => 120 ],
                'contentOptions' => ['width' => 120],
            ],
            [
                'headerOptions' => [ 'style' => 'text-align:center;' , 'width' => 100 ],
                'contentOptions' => ['width' => 100],
                'attribute' => 'task_type',
                'label' => '类型',
            ],
            [
                'headerOptions' => [ 'style' => 'text-align:center;' , 'width' => 50 ],
                'contentOptions' => ['width' => 50],
                'label' => '进度',
                'value' => function ($data) {
                    if($data['is_fixed'] == 1 || $data['total_limit_count'] == '0'){
                        return '0%';
                    }else{
                        return number_format( $data['finish_count']/$data['total_limit_count']*100 , 2 ).'%';
                    }
                }
            ],
            [
                'headerOptions' => [ 'style' => 'text-align:center;' , 'width' => 65 ],
                'contentOptions' => ['width' => 65],
                'label' => '完成/限制(次数)',
                'value' => function ($data) {
                    return $data['finish_count']."/".($data['total_limit_count'] > 0 ? $data['total_limit_count'] : '不限');
                }
            ],
            [
                'headerOptions' => [ 'style' => 'text-align:center;' , 'width' => 60 ],
                'contentOptions' => ['width' => 60],
//                'attribute' => 'single_reward',
                'label' => '单次奖励(金币)',
                'value' => function($data){
                    if($data['task_type'] == '签到任务'){
                        return '10-40';
                    }else{
                        return $data['single_reward'];
                    }
                }
            ],
            [
                'headerOptions' => [ 'style' => 'text-align:center;' , 'width' => 60 ],
                'contentOptions' => ['width' => 60],
                'label' => '合计奖励(金币)',
                'value' => function ($data) {
                    return $data['finish_count'] * $data['single_reward'];
                }
            ],
            [
                'headerOptions' => [ 'style' => 'text-align:center;' , 'width' => 60 ],
                'contentOptions' => ['width' => 60],
//                'attribute' => 'real_price',
                'label' => '实际单价(元)',
                'value' => function($data){
                    if($data['is_fixed'] == '1'){
                        return '0';
                    }else{
                        return $data['real_price'];
                    }
                }
            ],
            [
                'headerOptions' => [ 'style' => 'text-align:center;' , 'width' => 60 ],
                'contentOptions' => ['width' => 60],
                'label' => '实际收入(元)',
                'value' => function($data){
                    if($data['is_fixed'] == '1'){
                        return '0';
                    }else{
                        return $data['finish_count'] * $data['real_price'];
                    }
                }
            ],
            [
                'headerOptions' => [ 'style' => 'text-align:center;' , 'width' => 90 ],
                'contentOptions' => ['width' => 90],
                'label' => '预支出参考范围(元)',
            ],
            [
                'headerOptions' => [ 'style' => 'text-align:center;' , 'width' => 95 ],
                'contentOptions' => ['width' => 95],
                'label' => '开始时间',
                'value' => function ($data) {
                    if($data['start_time'] == '0000-00-00 00:00:00'){
                        return '-';
                    }else{
                        return $data['start_time'];
                    }
                }
            ],
            [
                'headerOptions' => [ 'style' => 'text-align:center;' , 'width' => 95 ],
                'contentOptions' => ['width' => 95],
                'label' => '结束时间',
                'value' => function($data){
                    if($data['end_time'] == '0000-00-00 00:00:00'){
                        return '-';
                    }else{
                        return $data['end_time'];
                    }
                }
            ],
            [
                'headerOptions' => [ 'style' => 'text-align:center;' , 'width' => 60 ],
                'contentOptions' => ['width' => 60],
                'label' => '状态',
                'value' => function ($data) {
                    switch($data['put_in_status']){
                        case 0:
                            return '关闭';
                        case 1:
                            return '投放中';
                    }
                }
            ],
            [
                'headerOptions' => [ 'style' => 'text-align:center;' , 'width' => 150 ],
                'contentOptions' => ['width' => 150],
                'class' => \yii\grid\ActionColumn::className(),
                'header' => '操作',
                'contentOptions' => ['class' => 'action-link'],
                'template' => '{close} {delete} {update}',
                'buttons' => [
                    'close' => function ($url, $model, $key) {
                        if($model['put_in_status'] == '0'){
                            return "<a href='/task/open?id=".$model['id']."'>开启</a>";
                        }elseif($model['put_in_status'] == '1'){
                            return "<a href='/task/close?id=".$model['id']."'>关闭</a>";
                        }
                    },
                    'delete' => function ($url, $model, $key) {
                        return "<a href='/task/delete?id=".$model['id']."'>删除</a>";
                    },
                    'update' => function ($url, $model, $key) {
                        return "<a href='/task/create?id=".$model['id']."'>编辑</a>";
                    },
                ],
            ],
        ],
    ]);
    ?>
    <div style="float: right;margin-right: 1.7%;">
        <?php echo LinkPager::widget(['pagination' => $pagination]) ?>
    </div>
</div>
<script>
    var myDate = new Date(); //获取今天日期
//    alert(myDate)
//    var myYear = myDate.getFullYear() //获取年份
//    myDate.setDate(myDate.getDate() - 7);
//    var dateArray = [];
//    var dateTemp;
//    var flag = 1;
//    for (var i = 0; i < 7; i++) {
//        dateTemp = (myDate.getMonth() + 1) + "-" + myDate.getDate();
//        dateArray.push(dateTemp);
//        myDate.setDate(myDate.getDate() + flag);
//    }
//    var Start = myYear + "-" + dateArray[6]
    var Start = moment(myDate).format("YYYY-MM-DD");
    var S = "<?php echo $select['start'] ? date('Y-m-d', strtotime($select['start'])) : ''; ?>";
    var E = "<?php echo $select['end'] ? date('Y-m-d', strtotime($select['end'])) : ''; ?>";
    if (S == '') {
        $("#reservation").val(Start + " " + "-" + " " + Start)
        S = Start;
    } else {
        $("#reservation").val(S + " " + "-" + " " + E)
    }
    $('#reservation').daterangepicker(null, function (start, end, label) {
        S = moment(start).format("YYYY-MM-DD");
        E = moment(end).format("YYYY-MM-DD");
        $("#reservation").val(S + " " + "-" + " " + E)
    });
    
    $("#type").change(function(){
        var type = $(this).val();
        var title = $("#title").val();
        location.href="/task/task-list?title="+title+"&type="+type;
    });
    
    $(".SS").click(function(){
        var type = $("#type").val();
        var title = $("#title").val();
        location.href="/task/task-list?title="+title+"&type="+type;
    });
    
    $(".DD").click(function(){
        var keys = [];
        $(":checkbox:checked").each(function(){
            keys.push($(this).val());
        });
        if(keys.length == 0){
            return false;
        }
        $.ajax({
            url:'/task/close',
            type:'post',
            dataType:'json',
            data:{id:keys},
            success:function(data){
                alert(data.msg);
                location.reload();
            },
            error:function(e){
                alert('请求失败！');
            }
        });
    });
    
</script>
